﻿using System.Xml;
using System.Web;
using uForum.Configuration;
using umbraco;

namespace uForum
{
    public class SearchForTopic : umbraco.interfaces.INotFoundHandler
    {
        #region INotFoundHandler Members

        private int _redirectId = -1;

        public bool CacheUrl
        {
            get { return false; }
        }

        public bool Execute(string url)
        {

            HttpContext.Current.Trace.Write("uForum.urlHandler", "init");

            bool succes = false;
            url = url.Replace(".aspx", string.Empty);
            if (url.Length > 0)
            {
                if (url.Substring(0, 1) == "/")
                    url = url.Substring(1, url.Length - 1);

                XmlNode urlNode = null;
                string topicId = "";
                string topicTitle = "";

                // We're not at domain root
                if (url.IndexOf("/") != -1)
                {

                    string theRealUrl = url.Substring(0, url.LastIndexOf("/"));
                    string realUrlXPath = requestHandler.CreateXPathQuery(theRealUrl, true);

                    urlNode = content.Instance.XmlContent.SelectSingleNode(realUrlXPath);
                    topicTitle = url.Substring(url.LastIndexOf("/") + 1, url.Length - url.LastIndexOf(("/")) - 1).ToLower();
                    topicId = topicTitle.Split('-')[0];
                    topicTitle = topicTitle.Substring(topicTitle.IndexOf('-') + 1);

                    HttpContext.Current.Trace.Write("uForum.urlHandler", string.Format("Found url for topic with id {0} and title {1}", topicId, topicTitle));
                }


                if (urlNode != null)
                {
                    if (topicId != "")
                    {
                        XmlAttribute legacyNodeTypeAliasAttribute = urlNode.Attributes["nodeTypeAlias"];
                        string nodeTypeAlias = legacyNodeTypeAliasAttribute == null ? string.Empty : legacyNodeTypeAliasAttribute.Value;
                        if (urlNode.Name == ContentProperties.ForumDocumentTypeAlias || nodeTypeAlias == ContentProperties.ForumDocumentTypeAlias)
                        {
                            _redirectId = int.Parse(urlNode.Attributes.GetNamedItem("id").Value);

                            HttpContext.Current.Items["altTemplate"] =
                                ContentProperties.DisplayTopicTemplateAlias.ToLower();
                            HttpContext.Current.Items[ContentProperties.TopicIdHttpKey] = topicId;

                            HttpContext.Current.Trace.Write("uForum.urlHandler",
                                                            string.Format("Templated changed to: '{0}'",
                                                                          HttpContext.Current.Items["altTemplate"]));
                            succes = true;
                        }
                    }
                }
            }
            return succes;
        }

        public int redirectID
        {
            get
            {
                return _redirectId;
            }

        }

        #endregion
    }
}
